﻿
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX)
#define DISABLESTEAMWORKS
#endif
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class LogsPanel : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public InputField m_Command;
    public Text m_MessageProfab;
    public GameObject m_MessageParent;
    public GameObject m_Panel;
    float m_ShowTimeCount = 0;
    float m_ShowDuration=5;
    bool m_CountSwitch = true;

    void Start()
    {
        MessageShow.Instance.OnMessageCallback = AddMessage;
    }

    void Awake()
    {
        m_Command.onEndEdit.AddListener(OnCommandComplate);
    }
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            OnExpandClicked();
        }
        if(m_Panel.activeSelf)
        {
            if(m_CountSwitch)
                m_ShowTimeCount += Time.deltaTime;
            if(m_ShowTimeCount > m_ShowDuration)
            {
                m_ShowTimeCount = 0;
                m_Panel.SetActive(false);
            }
        }
    }
    public void AddMessage(string value)
    { 
        var profab = GameObject.Instantiate(m_MessageProfab);
        profab.text = value;
        profab.transform.SetParent(m_MessageParent.transform);
        profab.transform.localScale = new Vector3(1,1,1);
        m_Panel.SetActive(true);
    }
    public void OnCommandComplate(string value)
    {
        if (value != "" && Player.Instance.m_PlayerNetwork)
            Player.Instance.m_PlayerNetwork.CmdSendPlayerMessage( Commander.Instance.m_Name + "：" + value);

        m_ShowTimeCount = 0;
        if(OnlineManager.singleton && !OnlineManager.singleton.isNetworkActive)
            MessageShow.Instance.Messaage(value);
        m_Command.text = "";

#if !DISABLESTEAMWORKS
        if (value == "ResetAllStats" && UtilsGame.IsSteamMG())
            Steamworks.SteamUserStats.ResetAllStats(true);
#endif
        if (value == "OpenDevelopmentMode")
            Player.Instance.m_DevelopmentMode = true;
        if (value == "AddMoney")
            Player.Instance.Money = 1000000;
        if (value == "ClearMoney")
            Player.Instance.Money = 0;
        if (value == "MaxDriveLicense")
            Player.Instance.m_DriveLevel = Driver.DriveLevel.LEVEL_AP;
        if (value == "MaxBattleLevel")
            Player.Instance.m_BattleLevel = 10;
        if (value == "BecomeMember")
            Commander.Instance.m_Member = 1;
    }

    public void OnExpandClicked()
    {

        m_Panel.SetActive(!m_Panel.activeSelf);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        m_CountSwitch = false;
    }

    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
        m_CountSwitch = true;
    }

    private void OnDestroy()
    {
        MessageShow.Instance.OnMessageCallback = null;
    }
}
